<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";

require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    },
    setOptions({ label, riskall, riskall_title, riskywc, riskywc_title } = {}) {
      this.chart.setOption({
        title: {
          text: '近6个月安全隐患',
          left: 'left',
          padding: [
            10, // 上
            10, // 右
            10, // 下
            10 // 左
          ],
          textStyle: { color: '#606266', fontSize: '14' }
        },
        xAxis: {
          data: label,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          nameTextStyle: {
            color: '#43425D'
          },
          axisLine: {
            show: false,
            lineStyle: {}
          },
          axisLabel: {
            textStyle: {
              color: "#606266", //文字的颜色
            },
          },
        },
        grid: {
          left: 10,
          right: 30,
          bottom: 20,
          top: 60,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              color: "#606266", //文字的颜色
            },
          },
        },
        legend: {
          right: '10',
          top: '10',
          data: [riskall_title, riskywc_title],
          textStyle: { color: "#606266" },
        },
        series: [
          {
            name: riskall_title,
            itemStyle: {
              color: '#a4a1fb',
            },
            lineStyle: {
              // color: '#FF005A',
              width: 2
            },
            areaStyle: {
              opacity: 0.5,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                { offset: 0, color: '#fff' },
                { offset: 1, color: 'rgba(163,160,251,0.8)' }
              ]),

            },
            smooth: true,
            type: 'line',
            data: riskall,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: riskywc_title,
            smooth: true,
            type: 'line',
            itemStyle: {
              color: '#54D8FF',
            },
            lineStyle: {
              // color: '#3888fa',
              width: 2
            },
            areaStyle: {
              opacity: 0.5,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                { offset: 0, color: '#fff' },
                { offset: 1, color: 'rgba(84,216,255,0.7)' }
              ]),

            },
            data: riskywc,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }
        ]
      })
    }
  }
}
</script>
